package mvp.cklm.testtask.util;

import mvp.cklm.testtask.service.IService;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: valera
 * Date: 25.02.14
 * Time: 7:52
 */
public class ServiceKeeper {
    private static Map<String, IService> services;

    static {
        services = Collections.synchronizedMap(new HashMap<String, IService>());
    }

    public static void registerService(IService service) {
        Class<? extends IService> servClass = service.getClass();
        if (servClass.isInterface()) {
            services.put(servClass.getName(), service);
        } else {
            Class interfs[] = servClass.getInterfaces();
            for (int i = 0; i < interfs.length; i++) {
                if (IService.class.isAssignableFrom(interfs[i])) {
                    services.put(interfs[i].getName(), service);
                }
            }
        }
    }

    public static IService getService(Class<? extends IService> serviceClass) {
        IService toSend = services.get(serviceClass.getName());
        return toSend;
    }
}
